/*
 * AbstractBoxBot.java
 *
 * Created on 28. Januar 2007, 17:37
 *
 * Copyright (c) 2007 Daniel Gronau
 *
 * This file is part of KanjiTori.
 *
 * KanjiTori is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * KanjiTori is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
 *
 */

package kanjitori.graphics.bot;

import com.jme.bounding.BoundingBox;
import com.jme.math.Vector3f;
import com.jme.renderer.ColorRGBA;
import com.jme.scene.Controller;
import com.jme.scene.Node;
import com.jme.scene.shape.RoundedBox;
import com.jme.scene.state.CullState;
import com.jme.scene.state.MaterialState;
import java.awt.Image;
import kanjitori.util.GraphicUtil;

/**
 *
 * @author Pirx
 */
public abstract class AbstractBoxBot extends AbstractBot {

    private final static ColorRGBA MAT_COLOR = new ColorRGBA(1, 1, 1, 0);
    
    private MaterialState ms;
    
    /** Creates a new instance of AbstractBoxBot 
     * @param value 
     */
    public AbstractBoxBot(String value) {
        super(value);
    }
    
    void init() {
        node = new Node("bot");
        Image img = getImage(value);
        
        RoundedBox box = new RoundedBox("bot",
                new Vector3f(0.5f, 0.5f, 0.5f),
                new Vector3f(0.05f, 0.05f, 0.05f),
                new Vector3f(0.02f, 0.02f, 0.02f));
        box.setRenderState(ms = GraphicUtil.getMaterialState(MAT_COLOR));
        box.setRenderState(GraphicUtil.loadTexture(img));
        box.setRenderState(GraphicUtil.getCullState(CullState.CS_BACK));
        box.updateRenderState();
        box.setModelBound(new BoundingBox());
        box.updateModelBound();
        node.attachChild(box);
    }    

    public void die() {
       
        node.getChild(0).setRenderState(GraphicUtil.getTransparence());  
        node.setRenderQueueMode(display().getRenderer().QUEUE_TRANSPARENT);
        node.updateRenderState();
                
        node.addController(new Controller() {
            private float time = 0;
            public void update(float f) {
                time += f;
                ms.getDiffuse().a = (2 - time) / 2;
                if (time > 2) {
                    GraphicUtil.removeControllers(node);
                    remove();
                    GraphicUtil.cleanTexture(node);
                }
            }
        });
    }    
    
    abstract Image getImage(String value);

}
